<?php

/**
 * 
 * @author Administrator
 *
 */
class TitleService extends Service{

    public static function indexHeader(){
        return array(
        		'id'         => array('text' => 'ID', 'rule' => 'int'),
        		't_id'       => array('text' => '称号标识'),
        		'name'       => array('text' => '称号名称'), 
        		'type_name'  => array('text' => '大类名字'), 
        		'access'     => array('text' => '获取途径'),
                'action'     => array('text' => '操作')
        );
    }
    
    public static function indexData($params){
    	$modTitle = new ModTitle();
    	$result = $modTitle->page($params); 
    	
    	foreach($result['data'] as &$v){ 
    		$v['action']   .= ' <a href="'.PubFunc::buildUrl('/oss/?r=title/delete').'&id='.$v['id'].'" confirm="是否要删除该条信息?">删除</a>';
    	}
    	unset($v);
    	return $result;
    }
    
    
    
    public static function delete($id){
    	if(empty($id)) return false;
    
    	$modTitle = new ModTitle();
    	$modTitle->deleteByPk($id);
    	return true;
    }
    
    public static function upload($files){
    	$file = new PubFile();
    	$fileName = $files["file"]["name"];
    	$suffixArr = array('xls','xlsx','xlsm'); //允许上传的文件后缀
    	$check= $file::checkSuffix($fileName,$suffixArr);
    	if($check){
    		$ext = $file::getSuffix($fileName);
    		$uploadDir = '../public/upload/title/'.date('Ym').'/';
    		$uploadName = md5(microtime(true)).'.'.$ext;
    		$upload = $file::uploadFile($files["file"],$uploadDir,$uploadName);
    		if(!$upload){
    			return array('status' => 0, 'msg' =>$upload,'data'=>'');
    		}else{
    			$path = dirname(dirname(dirname(dirname(__File__)))).'/public/upload/title/'.date('Ym').'/'.$uploadName;  //获取绝对路径
    			return array('status' => 1, 'msg' =>'上传成功','data'=>$path);
    		}
    	}else{
    		return array('status' => 0, 'msg' =>'上传文件后缀不允许!','data'=>'');
    	}
    }
    
    public static function xlsAdd($data){
    	$modTitle = new ModTitle();
    	unset($data[1]);
    	unset($data[2]);
    	unset($data[3]);
    	unset($data[4]);
    	
    	$add_data = array();
    	$gid_array = $modTitle->getGid();
    	
    	$gid_in = array();
    	foreach ($gid_array as $key=>$val){
    		$gid_in[] = $val['t_id'];
    	}
    	$game = PubBase::getCurrentGame();
        $all_num = count($data);
        $success_num = 0;
        $error_num = 0;
    	foreach($data as $v){
    		if(in_array($v['A'], $gid_in)){  //已经存在
    			$error_num++;
    			continue;
    		}
    		$d = array();
    		$d['game'] = $game;
    		$d['t_id'] = $v['A'];
    		$d['default_display'] = $v['B'];
    		$d['name'] = $v['C'];
    		$d['rank'] = $v['D'];
    		$d['type'] = $v['E'];
    		$d['type_name'] = $v['F'];
    		$d['times'] = $v['G'];
    		$d['act_props'] = $v['H'];
    		$d['attribute_id'] = $v['I'];
    		$d['resource_id'] = $v['J'];
    		$d['access'] = $v['K'];
    		$d['hearsay_id'] = $v['L'];

    		array_push($add_data, $d);
    	}
    	$success_num = $all_num - $error_num;
    	$msg = '导入成功 '.$success_num.' 条，失败 '.$error_num.' 条';
    	$result = $modTitle->addmuch($add_data);
    	return array('status' => 1, 'msg' =>$msg,'data'=>'');
    }
    
    

    
}